package bx;

import akka.actor.typed.ActorRef;
import akka.actor.typed.ActorSystem;
import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.Behaviors;
import bx.actor.WorkerRegistActor;
import bx.httpserver.AkkaJobWorkerServer;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class JobWorkerBootstrap {

    private static ActorRef<WorkerRegistActor.WorkerRegistActorMsg> workerRegistActor;
    private static ActorSystem<Void> actorSystem;

    // 全局延迟调度器，不要在其中执行长时间阻塞操作，仅仅执行延迟 tell 发消息等轻量级操作
    private static final ScheduledExecutorService GLOBAL_SCHEDULED_EXECUTOR_SERVICE = Executors.newScheduledThreadPool(1);

    public static void main(String[] args) {
        bootstrap();
    }


    public static void bootstrap() {
        initActorSystem();
    }

    public static ScheduledExecutorService getGlobalScheduledExecutorService() {
        return GLOBAL_SCHEDULED_EXECUTOR_SERVICE;
    }


    private static void initActorSystem() {
        actorSystem = ActorSystem.create(JobWorkerBootstrap.create(), "bx-job-worker-ActorSystem");
    }

    private static Behavior<Void> create() {
        return Behaviors.setup(context -> {
            AkkaJobWorkerServer.startWorkerServer(context);  // 启动 worker 端 HTTP Server
            workerRegistActor = context.spawn(WorkerRegistActor.create(getJobServerList()), "bx-job-WorkerRegistActor");
            return Behaviors.receive(Void.class).build();
        });
    }

    private static List<String> getJobServerList() {
        return Lists.newArrayList("http://127.0.0.1:9990");
    }
}